<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-form-item label="计划名称" prop="name">
        <el-input v-model="formItem.name" placeholder="请输入计划名称" />
      </el-form-item>
      <el-form-item label="计划日期" prop="dateSets">
        <el-date-picker
          v-model="formItem.dateSets"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="说明" prop="remarks">
        <el-input v-model="formItem.remarks" placeholder="请输入说明" maxlength="1023" type="textarea" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import belongs from '@neu/dcam-ui/mixins/belongs'
import {
  create,
  update,
  detail
} from '@neu/dcam-ui/api/inventoryPlan'

// 定义一个表单原始项
const formItem = {
  name: '',
  startDate: '',
  endDate: '',
  dateSets: [],
  remarks: '',
  status: 'created',
  belongs: ''
}
export default {
  name: 'InventoryPlanEditForm',
  mixins: [belongs],
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      /** 是否显示用户选择对话框 */
      userSelDialogFormVisible: false,
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        name: [{
          required: true,
          message: '计划名称不能为空',
          trigger: 'blur'
        },
        {
          validator: function(rule, value, callback) {
            if (value.length > 50) {
              callback(new Error('最多可输入50个字符'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }],
        dateSets: [{
          required: true,
          message: '日期不能为空',
          trigger: 'blur'
        }]
      },
      modal: true
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler(newVal, oldVal) {
        this.fetch()
      },
      immediate: true
    }
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data,
            dateSets: [response.data.startDate, response.data.endDate]
          }
        })
      } else {
        this.formItem = {
          ...formItem,
          belongs: this.belongs
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.id !== -1

      if (this.formItem.dateSets.length !== 0) {
        this.formItem.startDate = this.formItem.dateSets[0]
        this.formItem.endDate = this.formItem.dateSets[1]
      }

      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    }
  }
}
</script>
